package com.zzsy.shoppingpro.home.fragment;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.app.Config;
import com.zzsy.shoppingpro.base.BaseFragment;
import com.zzsy.shoppingpro.basebean.BaseBean;
import com.zzsy.shoppingpro.home.bean.HomeBean;
import com.zzsy.shoppingpro.home.adapter.HomeRecycleAdapter;
import com.zzsy.shoppingpro.http.BaseCallback;
import com.zzsy.shoppingpro.http.RetrofitHelper;
import com.zzsy.shoppingpro.server.MainService;
import com.zzsy.shoppingpro.utils.Toast;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/8/17 0017.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    private EditText etSeachMsg;
    private ImageView imgSeach;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView rvHome;
    private ImageButton imgBtnTop;
    private HomeRecycleAdapter adapter;


    @Override
    public void initData() {
        // 请求网络数据
        getDataFromNet();
    }


    /**
     * 网络请求数据
     */
    private void getDataFromNet() {
        RetrofitHelper.create(MainService.class, Config.BASE_URL_JSON)
                .getHomeData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseCallback<BaseBean<HomeBean.ResultBean>>() {
                    @Override
                    public void success(BaseBean<HomeBean.ResultBean> homeBeanBaseBean) {
                        if (homeBeanBaseBean.getCode() != 200) {
                            Toast.show(getContext(), homeBeanBaseBean.getMsg());
                            return;
                        }
                        processData(homeBeanBaseBean);
                        refreshLayout.finishRefresh();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    /**
     * 首页数据列表展示
     */
    private void processData(BaseBean<HomeBean.ResultBean> resultBeanBaseBean) {
        adapter = new HomeRecycleAdapter(context, resultBeanBaseBean);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        rvHome.setAdapter(adapter);
        //设置到滑动到哪个位置的监听
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position <= 3) {
                    imgBtnTop.setVisibility(View.GONE);
                } else {
                    imgBtnTop.setVisibility(View.VISIBLE);
                }
                return 1;
            }
        });
        //设置网格布局
        rvHome.setLayoutManager(manager);

    }


    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.fragment_home, null);
        //消息搜索控件
        etSeachMsg = view.findViewById(R.id.et_seach_msg);
        //搜索按钮控件
        imgSeach = view.findViewById(R.id.img_seach);
        //刷新控件
        refreshLayout = view.findViewById(R.id.sm_refresh);
        //列表控件
        rvHome = view.findViewById(R.id.rv_home);
        // 置顶按钮
        imgBtnTop = view.findViewById(R.id.img_btn_top);
        imgBtnTop.setOnClickListener(this);
        imgSeach.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                // 请求网络数据
                getDataFromNet();
            }
        });
        refreshLayout.setEnableLoadMore(false);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_seach:
                break;
            case R.id.img_btn_top:
                rvHome.scrollToPosition(0);
                break;
        }
    }
}
